import {$host} from "./index";
import User from "../models/User";
import UserInfo from "../models/UserInfo";
import UpdateUserDto from "../utils/dto/user/updateUserDto";
import GetUsersByFilterDto from "../utils/dto/user/getUsersByFilterDto";
import changeCultureDto from "../utils/dto/user/changeCultureDto";

export const getUser = async () => {
    const response = await $host.get("api/User/GetUser")
    const data = response!.data
    return response!.status === 200
        ? User.init(data.userId, data.email, data.userName, data.roles, data.userInfoId, data.userPhotoId)
        : undefined
}

export const getUserById = async (id: string) => {
    const response = await $host.get(`api/User/GetUserInfoById/${id}`)
    const data = response!.data
    return response!.status === 200
        ? new UserInfo(data.Id, data.userName, data.firstName, data.lastName, data.countryName, data.patronymic, data.imageId)
        : undefined
}

export const getLeaderboard = async (dto: GetUsersByFilterDto) => {
    const params = new URLSearchParams();

    if (dto.filter)
        params.append('filter', dto.filter);
    params.append('pageNumber', dto.pageNumber.toString());
    params.append('pageSize', dto.pageSize.toString());

    return await $host.get(`api/User/GetLeaderboard?${params.toString()}`);
}

export const updateUserInfo = async (dto: UpdateUserDto) => {
    return await $host.patch('api/User/UpdateUserInfo', dto)
}

export const changeCulture = async (dto: changeCultureDto) => {
    return await $host.patch('api/User/ChangeCulture', dto)
}

